package com.rxnetwork.sample.mvp.toolbar

import android.content.Context
import android.support.v4.app.FragmentActivity
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import com.rxnetwork.sample.R

/**
 * 顶部状态栏
 */
class TopbarLayout : RelativeLayout, View.OnClickListener {

    private var mTitleTv: TextView? = null
    var leftImg: ImageView? = null
        private set
    private var mRightImg: ImageView? = null
    private var mCloseImg: ImageView? = null
    /**
     * @return 返回右边的Tv用于更多属性控制
     */
    var rightTv: TextView? = null
        private set
    var secondRightImg: ImageView? = null
        private set
    var topbarBgColor: Int = 0
        set(topbarBgColor) {
            field = topbarBgColor
            this.setBackgroundColor(this.topbarBgColor)
        }

    constructor(context: Context) : super(context) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    private fun init(context: Context) {
        View.inflate(context, R.layout.topbar_layout, this)
        mTitleTv = findViewById(R.id.title_tv) as TextView
        leftImg = findViewById(R.id.back_img) as ImageView
        mCloseImg = findViewById(R.id.close_img) as ImageView
        mRightImg = findViewById(R.id.right_img) as ImageView
        leftImg?.setOnClickListener(this)
        rightTv = findViewById(R.id.right_tv) as TextView
        secondRightImg = findViewById(R.id.right_second_img) as ImageView

        //        view.setBackgroundColor(mTopbarBgColor);
    }


    /**
     * 隐藏返回键
     */
    fun hideLeftImg() {
        leftImg?.visibility = View.GONE
    }

    fun setTitle(text: CharSequence) {
        if (!TextUtils.isEmpty(text))
            mTitleTv?.text = text
    }

    fun setTitle(resId: Int) {
        mTitleTv?.setText(resId)
    }

    fun setLeftImgClickListener(listener: View.OnClickListener) {
        leftImg?.setOnClickListener(listener)
    }

    fun setCloseImgShow(isShow: Boolean) {
        mCloseImg?.visibility = if (isShow) View.VISIBLE else View.GONE
    }

    fun setCloseImgListener(listener: View.OnClickListener) {
        mCloseImg?.setOnClickListener(listener)
    }

    /**
     * 设置右边图标的图片和事件

     * @param resId
     * *
     * @param listener
     */
    fun setRightImg(resId: Int, listener: View.OnClickListener) {
        mRightImg?.visibility = View.VISIBLE
        mRightImg?.setImageResource(resId)
        mRightImg?.setOnClickListener(listener)
    }

    fun setSecondRightImg(resId: Int, listener: View.OnClickListener) {
        secondRightImg?.visibility = View.VISIBLE
        secondRightImg?.setImageResource(resId)
        secondRightImg?.setOnClickListener(listener)
    }

    /**
     * 设置右边的TextView的行为

     * @param content
     * *
     * @param listener
     */
    fun setRightText(content: String, listener: View.OnClickListener) {
        rightTv?.visibility = View.VISIBLE
        rightTv?.text = content
        rightTv?.setOnClickListener(listener)
    }

    /**
     * 隐藏右边的TextView
     */
    fun hideRightText() {
        rightTv?.visibility = View.GONE
    }

    fun hideRightImg() {
        mRightImg?.visibility = View.GONE
    }

    fun setLeftImg(resId: Int) {
        leftImg?.setImageResource(resId)
    }

    override fun onClick(v: View) {
        if (context is FragmentActivity) {
            val activity = context as FragmentActivity
            //这里是取出我们返回栈存在Fragment的个数
            if (activity.supportFragmentManager.backStackEntryCount > 0) {
                //取出我们返回栈保存的Fragment,这里会从栈顶开始弹栈
                activity.supportFragmentManager.popBackStack()
            } else {
                activity.onBackPressed()
            }
        }
    }
}
